package com.xunyin.enums;

/**
 * @author jthong
 * @Description:控制板指令代码
 * @Version 1.0.0
 * @Date 2019/12/26 16:18
 */
public enum CommandCodeEnum {
    /***
     * 开锁功能码
     */
    OPEN(11, "开锁"),

    /***
     * 查询功能码
     */
    QUERY(33, "查询");

    private final String name;
    private final int value;

    CommandCodeEnum(int value, String name) {
        this.name = name;
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return value;
    }

    public static CommandCodeEnum parse(Integer value) {
        if (value == null) {
            return throwParseException(value);
        }
        for (CommandCodeEnum e : CommandCodeEnum.values()) {
            if (e.getValue() == value) {
                return e;
            }
        }
        return throwParseException(value);
    }

    private static CommandCodeEnum throwParseException(Integer value) {
        throw new IllegalArgumentException(value + " can not parse to " + CommandCodeEnum.class.getName());
    }

    public static String getNameByValue(Integer value) {
        if (value == null) {
            return "";
        }
        for (CommandCodeEnum e : CommandCodeEnum.values()) {
            if (e.getValue() == value) {
                return e.getName();
            }
        }
        return "";
    }
}
